package com.szsfm.vehiclemanagement.mvp.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.szsfm.vehiclemanagement.base.BasePresenter;
import com.szsfm.vehiclemanagement.bean.Latlng;
import com.szsfm.vehiclemanagement.listener.LocationListener;
import com.szsfm.vehiclemanagement.manager.BDLocationManager;
import com.szsfm.vehiclemanagement.mvp.model.ILocationModel;
import com.szsfm.vehiclemanagement.mvp.model.modelImpl.LocationModelImpl;
import com.szsfm.vehiclemanagement.mvp.view.ILocationView;

/**
 * Created by zhoubigbo on 2017/11/29.
 */

public class LocationPresenter extends BasePresenter implements  LocationListener {

    private ILocationModel locationModel;
    private ILocationView locationView;
    private BDLocationManager mLocationManager;
    private Context appContext;

    public LocationPresenter(ILocationView view, Context context) {
        this.locationView = view;
        this.locationModel = new LocationModelImpl();
        this.appContext = context;
    }


    public void startLocation() {
        if (this.locationView != null) {
            this.locationView.startLocation();
        }
        if (this.locationModel != null) {
            this.locationModel.startLocation(listener);
        }
    }

    @Override
    public void onReceiveLocation(double lat, double lng, String place) {
        if (TextUtils.isEmpty(place)) return;
        mLocationManager.stop();
        if (this.locationView != null) {
            Latlng latlng = Latlng.shareInstance();
            latlng.setLatitue(lat);
            latlng.setLongitue(lng);
            latlng.setPlace(place);
            this.locationView.locationResult(latlng);
        }

    }

    ILocationModel.ILocationModelListener listener = new ILocationModel.ILocationModelListener() {
        @Override
        public void init() {
            mLocationManager = BDLocationManager.getInstance();
            mLocationManager.init(appContext);
            mLocationManager.setLocationListener(LocationPresenter.this);
            mLocationManager.start();
        }
    };

}
